<template>
  <div >
    <el-card class="box-card">
      <!--      头部-->
      <div slot="header" class="header">
        <span>销售额类别占比</span>
        <el-radio-group v-model="radio" >
          <el-radio-button label="全部渠道:" disabled></el-radio-button>
          <el-radio-button label="online" >线上</el-radio-button>
          <el-radio-button label="shop" >门店</el-radio-button>
        </el-radio-group>
      </div>
      <!--      身体二echart-->
      <div>
        <div class="echart" ref="echart"></div>
      </div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import {mapState} from "vuex";
export default {
  name: "Observe",
  data(){
    return{
      radio:'online',
      echart:null,
    }
  },
  mounted(){
    this.echart=echarts.init(this.$refs.echart)
    this.echart.setOption({
      title: {
        text: "肯德基",
        subtext: 1048,
        left: "center",
        top: "center",
      },
      legend: {
        orient:'vertical',
        left: 'left'
      },
      series: [
        {
          name: '销售额类别占比',
          type: 'pie',
          //调整图形的内外半径
          radius: ['30%', '80%'],
          avoidLabelOverlap: true,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: true,
            position: 'outside'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '40',
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: true,
          },
          data: []
        }
      ]
    })
    this.echart.on('mouseover',  (params)=> {
        const {name,value} =params.data;
        this.echart.setOption({
          title:{
            text: name,
            subtext: value,
          }
        })
      })
  },
  computed: {
    ...mapState({
      saleRank:(state)=>{
        return state.home.echartData.saleRank;
      },
    })
  },
  watch:{
    saleRank(){
      //默认设置 online
      this.echart.setOption({
          title:{
            text: this.saleRank.online[0].name,
            subtext: this.saleRank.online[0].value,
          },
          series:[{
            data:this.saleRank.online,
          }]
        })
      this.echart.on('mouseover',  (params)=> {
        const {name,value} =params.data;
        this.echart.setOption({
          title:{
            text: name,
            subtext: value,
          }
        })
      });
    },
    radio(){
      this.echart.setOption({
        title:{
          text: this.radio=='online'?this.saleRank.online[0].name:this.saleRank.shop[0].name,
          subtext: this.radio=='online'?this.saleRank.online[0].value:this.saleRank.shop[0].value,
        },
        series:[{
          data:this.radio=='online'?this.saleRank.online:this.saleRank.shop,
        }]
      });
      this.echart.on('mouseover',  (params)=> {
        const {name,value} =params.data;
        this.echart.setOption({
          title:{
            text: name,
            subtext: value,
          }
        })
      });
    },
  },
}
</script>

<style scoped>
.box-card{
  margin-top:20px;
}
.header{
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.echart{
  width:100%;
  height:400px;
}
</style>
